package 剑指offer;

import leetcode.day_2020_12_14_12_21.TreeNode;

public class Offer36_JZ26 {
    TreeNode pre, head;

    public TreeNode Convert(TreeNode pRootOfTree) {
        if(pRootOfTree == null) {
            return null;
        }
        dfs(pRootOfTree);
//         head.left = pre;
//         pre.right = head;
        return head;
    }

    void dfs(TreeNode pRootOfTree) {
        if (pRootOfTree == null) {
            return;
        }
        dfs(pRootOfTree.left);
        if (pre != null) {
            pre.right = pRootOfTree;
        } else {
            head = pRootOfTree;
        }
        pRootOfTree.left = pre;
        pre = pRootOfTree;
        dfs(pRootOfTree.right);
    }
}
